package com.hylanda.pb.controller;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hylanda.pb.entity.ReportModel;
import com.hylanda.pb.entity.dict.ReportMode;
import com.hylanda.pb.model.ReportModelQo;
import com.hylanda.pb.service.HtmlService;
import com.hylanda.pb.service.PBAppService;
import com.hylanda.pb.service.ReportModelService;

@Controller
@RequestMapping("/reportModel")
public class ReportModelController {
    private static Logger logger = LoggerFactory.getLogger(ReportModelController.class);
    private static final String prefix = "pb/";
    @Autowired
    private ReportModelService reportModelService;
    @Autowired
	private PBAppService pbAppService;
    @Autowired
	private HtmlService htmlService;
    @RequestMapping("/index")
    public String index() throws Exception{
        return prefix+"reportmodel/index";
    }

    @RequestMapping(value="/{id}")
    public String show(ModelMap model,@PathVariable String id) {
        ReportModel reportModel = reportModelService.findById(id);
        model.addAttribute("reportmodel",reportModel);
        model.addAttribute("reportMode",ReportMode.getAllReportMode());
        return prefix+"reportmodel/show";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Page<ReportModel> getList(ReportModelQo reportModelQo) {
        try {
            return reportModelService.findPage(reportModelQo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/new")
    public String create(ModelMap model,ReportModel reportModel){
    	model.addAttribute("reportMode",ReportMode.getAllReportMode());
        return prefix+"reportmodel/new";
    }

    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(ReportModel reportModel) throws Exception{
        reportModelService.create(reportModel);
        logger.info("新增->ID="+reportModel.getProcessorId());
        return "1";
    }

    @RequestMapping(value="/edit/{id}")
    public String update(ModelMap model,@PathVariable String id){
        ReportModel reportModel = reportModelService.findById(id);
        model.addAttribute("reportMode",ReportMode.getAllReportMode());
        model.addAttribute("reportmodel",reportModel);
        return prefix+"reportmodel/edit";
    }

    @RequestMapping(method = RequestMethod.POST, value="/update")
    @ResponseBody
    public String update(ReportModel reportModel) throws Exception{
        reportModelService.update(reportModel);
        logger.info("修改->ID="+reportModel.getProcessorId());
        return "1";
    }

    @RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable Long id) throws Exception{
        reportModelService.delete(id);
        logger.info("删除->ID="+id);
        return "1";
    }
    @RequestMapping(value="/report/display/{id}",method = RequestMethod.GET)
    public String reportView(HttpServletRequest request, @PathVariable String id,HttpServletResponse response) throws Exception{
    	 Map<String, Object> map = this.pbAppService.getReportModelByInstanceId(id);
    	 String report_type=(String)map.get("report_type");
    	 if("local".equals(report_type)){
    		return "redirect:/reports/powerbi/"+id;
    	 }else{
    		 return "redirect:/reportmodel/get/"+id+"/diffByPid/"+createNewToken(appId);
    	 }
		
    }
    
    public static final String appId="02bbdbef98dd406fadcbb21ee6dc62e3";
    public static String createNewToken(String appId) {
  		// 获取当前时间
  		Date now = new Date(System.currentTimeMillis());
  		// 过期时间
  		Date expiration = new Date(now.getTime() + 7200000 * 12);// 1天后过期
  		return Jwts
  				.builder()
  				.setSubject(appId)
  				// .claim(YAuthConstants.Y_AUTH_ROLES, userDbInfo.getRoles())
  				.setIssuedAt(now).setIssuer("Online YAuth Builder")
  				.setExpiration(expiration)
  				.signWith(SignatureAlgorithm.HS256, "valleyAuthv1.0.0")
  				.compact().replace(".", "@");
  	}
}
